//检测数据
function buy(selector, b, type) {
    $.ajax({
        type: "get",
        url: USER_SITE_URL + "/index.php?app=member&act=Ajax_islogin&jsoncallback=?",
        dataType: "jsonp",
        success: function(data) {
            if (data.done) {
                var totalnum = parseInt($('#totalnum').text());
                var goods_info_top = $('.goods_info').offset().top;
                if (totalnum > 0) {
                    add_to_cart(selector, b, type);
                    return;
                } else {
                    $(window).scrollTop(goods_info_top);
                    wl.sConfirm({
                        'info': '请至少采购1件商品',
                        'cancel': true
                    });
                    return;
                }
            } else {
                wl.showLogin();
            }
        }
    });
}

//添加到购物车
function add_to_cart(selector, b, type) {
        var cartJSON = [];
        var data = {};
        var goods_id = $('#goods_info').attr('goods_id');
        var cate_id = $('#goods_info').attr('cate_id');
        var goods_price_type = $('#goods_info').attr('goods_price_type');
        var inputs = $('table.specific').find('input.spec_input');
        inputs.each(function(i, e) {
            var _v = $(this).val();
            if (_v > 0) {
                var spec_id = $(this).closest('tr').attr('spec_id');
                data[i] = {
                    'spec_id': spec_id,
                    'quantity': _v
                };
            }
        });
        var str = JSON.stringify(data);
        var url = CART_SITE_URL + '/index.php?app=cart&act=add&jsoncallback=?';
        cartJSON = {
            'spec_quantity': str,
            'goods_id': goods_id,
            'goods_price_type': goods_price_type
        };
        $.getJSON(url, cartJSON, function(data) {
            if (data.done) {
                if (type === '1') {
                    //ignore ie6
                    if ($('.sidebar').css('position') === 'fixed') {
                        $(selector).css('visibility', 'visible');
                        b.start();
                    } else {
                        window.location.href = CART_SITE_URL + '/index.php?app=cart&act=add_succeed&cate_id=' + cate_id + '&goods_id=' + goods_id;
                    }
                } else {
                    window.location.href = CART_SITE_URL + '/index.php?app=cart';
                }
            } else {
                alert(data.msg);
            }
        });
    }
    //产品数量输入
function changeQuantity(spec_id, spec_tpey, input) {
        input.value = input.value.replace(/\D/g, '');
        var totalnum_span = $('#totalnum');
        var totalprice_span = $('#totalprice');
        var item = $('#input_item_' + spec_id);
        var spec = $('#spec_item_' + spec_id);
        var spec_price = parseInt(spec.attr('price'));
        var stock = parseInt(spec.attr('stock'));
        var price = 0;
        var totalprice = 0;
        var quantity = parseInt(item.val());
        if (quantity > stock) {
            item.val(stock);
            item.next('.ku').show();
            setTimeout(function() {
                item.next('.ku').hide();
            }, 1000);
        }
        var good_count = sumGoodCount();
        if (spec_price) {
            totalprice = wl.number_format(sumGoodPrice(), 2);
        } else {
            price = getPrice(good_count);
            totalprice = wl.number_format(good_count * price, 2);
        }
        totalnum_span.html(good_count);
        totalprice_span.html(totalprice);
        selectSpec(spec_tpey);

    }
    //产品数量减1
function decreaseQuantity(spec_id) {
        var item = $('#input_item_' + spec_id);
        var orig = parseInt(item.val());
        if (orig > 0) {
            item.val(orig - 1);
            item.keyup();
        }
    }
    //产品数量加1
function addQuantity(spec_id) {
        var item = $('#input_item_' + spec_id);
        var orig = parseInt(item.val());
        item.val(orig + 1);
        item.keyup();
    }
    //计算商品总数
function sumGoodCount() {
    var count = 0;
    $('.spec_list .spec_input:input').each(function() {
        if (parseInt($(this).val()) > 0) {
            count += parseInt($(this).val());
        }
    });
    return count;
}

//计算商品总价格
function sumGoodPrice() {
    var count = 0;
    $('.spec_list').each(function() {
        if (parseInt($(this).find('.spec_input:input').val()) > 0) {
            count += parseInt($(this).find('.spec_input:input').val()) * $(this).attr('price');
        }
    });
    return count;
}

//选中规格
function selectSpec(spec_tpey) {
    var count = 0;
    var select_s = $('#' + spec_tpey + '_i');
    $('.' + spec_tpey + ' .spec_input:input').each(function() {
        if (parseInt($(this).val()) > 0) {
            count += parseInt($(this).val());
        }
    });
    if (count > 0) {
        select_s.addClass('active');
    } else {
        select_s.removeClass('active');
    }
}

function getPrice(total) {
    var dls = $('#price-table-list > dl.bd1'),
        result = 0;
    dls.each(function() {
        $(this).removeClass('active');
        var price = parseFloat($(this).attr('price'));
        var min = parseInt($(this).attr('min_number'));
        if (min) {
            var max = parseInt($(this).attr('max_number'));
            if (total >= min && total <= max) {
                result = price;
                $(this).addClass('active');
            }
        } else {
            var number = parseInt($(this).attr('number'));
            if (total >= number) {
                result = price;
                $(this).addClass('active');
            }
        }
    });
    if (result == 0) {
        result = dls.eq(0).attr('price');
    }
    return result;
}

//商品信息,累计评价,交易安全说明切换
function selectMod(li) {
    li.click(function() {
        var index = li.index(this);
        if (index == 0) {
            $('#des_wrap').show();
            $('#J_consum').hide();
        }
        if (index == 1) {
            $('#des_wrap').hide();
            $('#J_consum').hide();
        }
        if(index ===2){
            if($('#J_consum > img:eq(0)').attr('srcinit')){
                $.each($('#J_consum > img'),function(){
                    $(this).attr('src',$(this).attr('srcinit')).removeAttr('srcinit');
                });
            }
            $('#des_wrap').hide();
            $('#J_consum').show();
        }
        li.eq(index).addClass('active').siblings('li').removeClass('active');
    });
}

$(function() {
    //放大镜效果/
    if ($(".jqzoom #goods_info_img").attr('jqimg')) {
        $(".jqzoom").jqueryzoom({
            xzoom: 475,
            yzoom: 475
        });
    }

    // 图片替换效果
    $('.ware_box li').mouseover(function() {
        $('.ware_box li').removeClass();
        $(this).addClass('ware_pic_hover');
        $('#goods_info_img').attr('src', $(this).children('img:first').attr('src'));
        $('#goods_info_img').attr('jqimg', $(this).attr('bigimg'));
    });

    //点击后移动的距离
    // var left_num = -61;

    //整个ul超出显示区域的尺寸
    // var li_length = ($('.ware_box li').width() + 6) * $('.ware_box li').length - 305;

    // $('.right_btn').click(function() {
    //     var posleft_num = $('.ware_box ul').position().left;
    //     if ($('.ware_box ul').position().left > -li_length) {
    //         $('.ware_box ul').css({
    //             'left': posleft_num + left_num
    //         });
    //     }
    // });

    // $('.left_btn').click(function() {
    //     var posleft_num = $('.ware_box ul').position().left;
    //     if ($('.ware_box ul').position().left < 0) {
    //         $('.ware_box ul').css({
    //             'left': posleft_num - left_num
    //         });
    //     }
    // });

    // 加入购物车弹出层
    // $('.close_btn').click(function() {
    //     $('.ware_cen').slideUp('slow');
    // });

    var tabNavLi = $('#tab_nav').find('li.J-taba-li');
    selectMod(tabNavLi);

    /*购物车飞入效果*/
    var parabola_load_img, p_offset_a, p_offset_b, p_offset_c, p_offset_d, parabola, parabola_setoption;
    if ($('.sidebar').css('position') === 'fixed') {
        parabola_load_img = function() {
            var parabola_img = $('#goods_info_img').attr('src');
            $('#J_parabola_el > img').attr('src', parabola_img);
            $('#J_fade_effect > img').attr('src', parabola_img);
        };
        parabola_setoption = function() {
            $('#J_parabola_el').css({
                'top': '10px',
                'left': '184px',
                'visibility': 'hidden'
            });
            p_offset_a = $('#J_parabola_el').offset().top;
            p_offset_b = $('#J_parabola_el').offset().left;
            p_offset_c = $('#J_cart_btn').offset().top;
            p_offset_d = $('#J_cart_btn').offset().left;
            parabola = new Parabola({
                el: "#J_parabola_el",
                offset: [(p_offset_d - p_offset_b + 2), -(p_offset_a - p_offset_c - 50)],
                curvature: 0.001,
                duration: 1300,
                callback: function() {
                    var totalnum = parseInt($('#totalnum').html());
                    var J_car_num = parseInt($('#J_car_num').html());
                    $('#J_car_num').html(totalnum + J_car_num);
                    $('#J_fade_effect').show().animate({
                        'top': 0,
                        'opacity': 0
                    }, 1000, function() {
                        $(this).hide().css({
                            'top': '95px',
                            'opacity': 1
                        });
                    });
                }
            });
        };
        parabola_load_img();
        parabola_setoption();
    }
    $('#J_buy,#shop_car_b').bind('click', function() {
        parabola_load_img();
        buy('#J_parabola_el', parabola, '1');
    });
    $('#J_pay_now').bind('click', function() {
        buy('#J_parabola_el', parabola, '2');
    });
    /*最新、最热*/
    jQuery('.goods-intro-ta').wlTabs('.goods-intro-ca > ul');
    /*滚动固定栏*/
    var tab_nav = $('#tab_nav');
    //购物车飞入效果 数据初始化
    $(window).bind('scroll', function() {
        if ($('.sidebar').css('position') === 'fixed') {
            wl.throttle(parabola_setoption, window);
        }
        var scrollTop = $(this).scrollTop();
        var left = ($(window).width() - 1200) / 2 + 214;
        if (scrollTop >= 857) {
            tab_nav.addClass('fixed').css({
                'top': 0,
                'left': left
            });
            $('#shop_car_b').show();
        } else {
            tab_nav.removeClass('fixed').css('left', 0);
            $('#shop_car_b').hide();
        }
    });
    $('.spec_input').eq(0).closest('li').next('li').children('a').get(0).click();
    /*end*/
});